package itd.servlets;

import itd.db.DBConnection;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class for Servlet: GetProductsList
 *
 */
 public class GetProductsList extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetProductsList() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String strError=null;
		HttpSession session 	= request.getSession();
		
		Vector vcProducts=null;

		try {

			vcProducts = DBConnection.getProductsList();
			
		} catch (Exception e){ 
			strError= e.getMessage();
		}
			
		if(strError!=null){
			request.setAttribute("strError",strError);
			getServletContext().getRequestDispatcher("/errorPage.jsp").forward(request, response);
		}else{
			session.setAttribute("vcProducts",vcProducts);
			getServletContext().getRequestDispatcher("/productList.jsp").forward(request, response);
		}
		
	}   	  	    
}